package com.chookss.video

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import com.chookss.R
import com.chookss.Urls
import com.chookss.home.studyPlan.StudyPlanAddActivity
import com.chookss.mine.personal.PersonalHomeActivity
import com.chookss.tools.MyEvent
import com.chookss.tools.MyToast
import com.chookss.tools.SignUtils2
import com.chookss.tools.Utils
import com.chookss.tools.okgoconfig.MyHttpRequest
import com.chookss.tools.okgoconfig.callback.ObserverCallback
import com.chookss.video.entity.VideoEntity
import com.chookss.video.model.VideoModel
import com.chookss.view.BottomListsDialog
import com.chookss.view.ForScreenInitDialog
import com.chookss.view.SelectDialogListener
import com.chookss.view.listener.ShareDialogListener
import com.chookss.wxapi.ShareActivity
import com.johnrambo.ktea.ktExtends.observe
import infoToast
import org.greenrobot.eventbus.EventBus
import org.json.JSONException
import org.json.JSONObject
import java.util.*
import kotlin.collections.HashMap

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/22 8:15
 * Note:
 */
open class OnVideoButtonClickListener(var videoModel: VideoModel, var lifecycle: LifecycleOwner) :
    LittleVideoListAdapter.OnItemBtnClick {

    override fun onVideoClick(v: View, tvComment: View, position: Int, entity: VideoEntity) {
        when (v.id) {
            R.id.ivAvatar -> {
                val bundle = Bundle()
                bundle.putString("employeeCode", entity.employeeCode)
//                val intent = Intent(v.context, PersonalHomeActivity::class.java)
                val intent = Intent(v.context, PersonalHomeActivity::class.java)
                intent.putExtras(bundle)
                v.context.startActivity(intent)
            }
            R.id.tvZan -> {
                var isLike = !v.isSelected
                videoModel.likeVideo(entity.videoId, entity.videoTitle, entity.employeeCode, isLike)
                    .observe(lifecycle, {
                        if (it.error == null) {
                            if (isLike) {
                                entity.videoLikeCounts = Utils.addNumber(entity.videoLikeCounts)
                            } else {
                                entity.videoLikeCounts =
                                    Utils.subtractionNumber(entity.videoLikeCounts)
                            }
                            entity.isLike = if (isLike) "1" else "0"
                            if (v.tag != null && v.tag is VideoEntity) {
                                var e1 = v.tag as VideoEntity
                                if (e1.videoId == entity.videoId) {
                                    (v as TextView).text = entity.videoLikeCounts.toString()
                                    v.isSelected = isLike
                                    var map: HashMap<String, String> = HashMap()
                                    map.put("videoId", entity.videoId)
                                    map.put("videoLikeCounts", entity.videoLikeCounts.toString())
                                    map.put("isLike", entity.isLike)
                                    EventBus.getDefault().post(
                                        MyEvent(
                                            map
                                        )
                                    )
                                }
                            }
                        }
                    })
            }
            R.id.tvCollect -> {
                var isCollect = !v.isSelected
                videoModel.collectVideo(entity.videoId, isCollect).observe(lifecycle, {
                    if (it.error == null) {
                        if (isCollect) {
                            entity.isCollect = "1"
                            entity.videoCollectCounts = Utils.addNumber(entity.videoCollectCounts)
                        } else {
                            entity.isCollect = "0"
                            entity.videoCollectCounts =
                                Utils.subtractionNumber(entity.videoCollectCounts)
                        }
                        if (v.tag != null && v.tag is VideoEntity) {
                            var e1 = v.tag as VideoEntity
                            if (e1.videoId == entity.videoId) {
                                (v as TextView).text = entity.videoCollectCounts.toString()
                                v.isSelected = isCollect
                                var map: HashMap<String, String> = HashMap()
                                map.put("videoId", entity.videoId)
                                map.put("videoCollectCounts", entity.videoCollectCounts.toString())
                                map.put("isCollect", entity.isCollect)
                                EventBus.getDefault().post(
                                    MyEvent(
                                        map
                                    )
                                )
                            }
                        }
                    }
                })
            }
            R.id.tvComment -> {
//                VideoCommentsDialog(v.context, entity).show()
                val dialog = VideoCommentsDialog(v.context, entity)
                dialog.changeComment("") { _, which ->
                    entity.videoCommentCounts = Utils.addNumber(entity.videoCommentCounts)
                    if (v.tag != null && v.tag is VideoEntity) {
                        var e1 = v.tag as VideoEntity
                        if (e1.videoId == entity.videoId) {
                            (v as TextView).text = entity.videoCommentCounts.toString()
                        }
                    }
                }
                dialog.show()
            }
            R.id.tvOnePlay -> {
                var dialog = VideoCommentsInputDialog(v.context, "1")
                dialog.inputAction = { text ->
                    videoModel.commentVideo(entity, text, { commentId ->
                        v.context.infoToast("评论成功")
                        entity.videoCommentCounts = Utils.addNumber(entity.videoCommentCounts)
                        if (v.tag != null && v.tag is VideoEntity) {
                            var e1 = v.tag as VideoEntity
                            if (e1.videoId == entity.videoId) {
                                (tvComment as TextView).text = entity.videoCommentCounts.toString()
                            }
                        }
                    }, {
                        v.context.infoToast(it)
                    })

                }
                dialog.show()

            }
            R.id.tvMmarquee -> {
                var intent = Intent(v.context, VideoCatesPlayActivity::class.java)
                intent.putExtra("watermark", "4")
                intent.putExtra("videoCate", entity.catalogCodes)
                v.context.startActivity(intent)
            }
            R.id.tvShare -> {
                var dialogList: ArrayList<String> = ArrayList()
                dialogList.add("分享")
                dialogList.add("在电脑上播放")
                dialogList.add("视频加入学习任务")
                if (!Utils.isNull(entity.videoCollectionsCode) && !"1".equals(entity.isSingle)) {
                    dialogList.add("集合加入学习任务")
                }
                dialogList.add("取消")
                var bottomListsDialog: BottomListsDialog? = null
                bottomListsDialog =
                    BottomListsDialog(v.context, dialogList, object : SelectDialogListener {
                        override fun onCancel() {}
                        override fun onListItemClick(position: Int, list: List<String>) {
                            if (position == 0) {
                                if ("0".equals(entity.isShare)) {
                                    MyToast.show("该视频暂不能分享")
                                    return;
                                }
                                val intent = Intent(v.context, ShareActivity::class.java)
                                intent.putExtra("url", entity.employeeCode)
                                intent.putExtra("title", entity.videoTitle)
                                intent.putExtra("createEmployeeName", entity.employeeName)
                                intent.putExtra("content", entity.videoKeyWord)
                                intent.putExtra("resourceId", entity.videoId)
                                intent.putExtra("picUrl", entity.videoCoverPath)
                                intent.putExtra("time", entity.videoSeconds)
                                intent.putExtra("videoShareCounts", entity.videoShareCounts)
                                intent.putExtra("resourceType", 3)
                                v.context.startActivity(intent)

                                entity.videoShareCounts = Utils.addNumber(entity.videoShareCounts)
                                if (v.tag != null && v.tag is VideoEntity) {
                                    var e1 = v.tag as VideoEntity
                                    if (e1.videoId == entity.videoId) {
                                        (v as TextView).text = entity.videoShareCounts.toString()
                                    }
                                }
                            } else if (position == 1) {
                                ForScreenInitDialog(v.context,
                                    ShareDialogListener {
                                        saveScan(entity.videoId)
                                    }).show()
                            } else if (position == 2) {
                                if ("0".equals(entity.isShare)) {
                                    MyToast.show("该视频暂不能加入学习任务")
                                    return;
                                }
                                val intent = Intent(v.context, StudyPlanAddActivity::class.java)
                                intent.putExtra("resourceId", entity.videoId)
                                intent.putExtra("resourceType", 1)
                                intent.putExtra("title", entity.videoTitle)
                                intent.putExtra("picUrl", entity.videoCoverPath)
                                intent.putExtra("time", entity.videoSeconds)
                                v.context.startActivity(intent)
                            } else if (position == 3) {
                                if (!Utils.isNull(entity.videoCollectionsCode) && !"1".equals(entity.isSingle)) {
                                    val intent = Intent(v.context, StudyPlanAddActivity::class.java)
                                    intent.putExtra("resourceId", entity.videoCollectionsCode)
                                    intent.putExtra("resourceType", 3)
                                    intent.putExtra("title", entity.videoCollectionsName)
                                    v.context.startActivity(intent)
                                }
                            }
                            bottomListsDialog!!.dismiss()
                        }
                    })
                bottomListsDialog.show()
            }
        }
    }

    fun saveScan(id: String) {
        val map: MutableMap<String, String> = java.util.HashMap()
        map["resourceId"] = id
        map["resourceType"] = "3"
        MyHttpRequest.getDefault().getRequestCompat(null, Urls.saveScan, SignUtils2.makeSignStr(map), false,
            object : ObserverCallback<String?> {
                override fun onError(errorMsg: String) {
                    MyToast.show(errorMsg)
                }
                override fun onSuccess(t: String?) {
                    var reObject: JSONObject? = null
                    try {
                        reObject = JSONObject(t)
                        if ("200".equals(reObject.getString("code"))) {
                        } else {
                            MyToast.show(reObject.getString("msg"))
                        }
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                }
            })
    }

}